package com.net.circleprogressexample;

import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.net.circleprogress.ArcProgress;
import com.net.circleprogress.CircleProgress;
import com.net.circleprogress.DonutProgress;
import com.net.numberprogress.NumberProgressBar;

import java.util.concurrent.Executor;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class MainActivity extends AppCompatActivity {

    private DonutProgress donutProgress1;
    private CircleProgress circleProgress1;
    private ArcProgress arcProgress1;

    private DonutProgress donutProgress2;
    private CircleProgress circleProgress2;
    private ArcProgress arcProgress2;

    private NumberProgressBar numberProgressBar1;
    private NumberProgressBar numberProgressBar2;
    private NumberProgressBar numberProgressBar3;
    private NumberProgressBar numberProgressBar4;
    private NumberProgressBar numberProgressBar5;
    private NumberProgressBar numberProgressBar6;
    private NumberProgressBar numberProgressBar7;
    private NumberProgressBar numberProgressBar8;

    private Task task;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        donutProgress1=findViewById(R.id.donutProgress1);
        circleProgress1=findViewById(R.id.circleProgress1);
        arcProgress1=findViewById(R.id.arcProgress1);


        donutProgress2=findViewById(R.id.donutProgress2);
        circleProgress2=findViewById(R.id.circleProgress2);
        arcProgress2=findViewById(R.id.arcProgress2);

        numberProgressBar1=findViewById(R.id.numberProgressBar1);
        numberProgressBar2=findViewById(R.id.numberProgressBar2);
        numberProgressBar3=findViewById(R.id.numberProgressBar3);
        numberProgressBar4=findViewById(R.id.numberProgressBar4);
        numberProgressBar5=findViewById(R.id.numberProgressBar5);
        numberProgressBar6=findViewById(R.id.numberProgressBar6);
        numberProgressBar7=findViewById(R.id.numberProgressBar7);
        numberProgressBar8=findViewById(R.id.numberProgressBar8);


        Executor executor = new ThreadPoolExecutor(3,6,10,
                TimeUnit.SECONDS,new LinkedBlockingDeque<Runnable>(5));
        task=new Task();
        task.executeOnExecutor(executor);





    }



    class Task extends AsyncTask<Void,Integer,Void>{



        public Task(){

        }

        @Override
        protected Void doInBackground(Void... voids) {
            for(int i=30;i<=100;i++){
                //如果isCancelled()返回true,则表示此任务已经被标记为cancle状态了，则此时立即停止后面的操作
                if(isCancelled()){
                    break;
                }
                publishProgress(i);
                try {
                    Thread.sleep(300);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            return null;
        }

        @Override
        protected void onProgressUpdate(Integer... values) {
            super.onProgressUpdate(values);
            if(isCancelled()){
                return;
            }
            donutProgress1.setProgress(values[0]);
            circleProgress1.setProgress(values[0]);
            arcProgress1.setProgress(values[0]);

            donutProgress2.setProgress(values[0]);
            circleProgress2.setProgress(values[0]);
            arcProgress2.setProgress(values[0]);

            numberProgressBar1.setProgress(values[0]);
            numberProgressBar2.setProgress(values[0]);
            numberProgressBar3.setProgress(values[0]);
            numberProgressBar4.setProgress(values[0]);
            numberProgressBar5.setProgress(values[0]);
            numberProgressBar6.setProgress(values[0]);
            numberProgressBar7.setProgress(values[0]);
            numberProgressBar8.setProgress(values[0]);



        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);

        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if(task!=null && task.getStatus()==AsyncTask.Status.RUNNING){
            //cancle()方法只是将对应的AsyncTask标记为cancle状态，并不是真正的取消线程的执行
            task.cancel(true);
        }
    }


}
